package www.study.com;

//整数反转 https://leetcode.cn/problems/reverse-integer/
public class code7 {
    public static void main(String[] args) {

    }

    /**
     * new StringBuilder(String.valueOf(x))
     * 注意new StringBuilder(x)中如果x是整数，意思是申请大小为x的空间，而不是将x这个数字变成字符形式
     * s.length() == 10 && s.compareTo(String.valueOf(Integer.MAX_VALUE))
     * 一定要加上s.length() == 10，否则会出现"23455"要大于"23222222222"的情况
     */
    class Solution {
        public int reverse(int x) {
            if(x == Integer.MIN_VALUE){
                return 0;
            }
            int up = x >= 0 ? 1 : -1;
            x = x >= 0 ? x : -x;
            String s = new StringBuilder(String.valueOf(x)).reverse().toString();
            System.out.println(s);
            if(s.length() == 10 && s.compareTo(String.valueOf(Integer.MAX_VALUE)) > 0) return 0;
            return Integer.parseInt(s) * up;
        }
    }
}
